#!/bin/bash

# hrindex *.chk

# 计算 H-R-Index，需要基态的*.chk文件，./tdopt/*_freq.chk文件（计算freq）

# 检查是否有参数传入
usage() {
    echo "Usage: $0 [-h] baseline_chk_file"
    echo
    echo "计算 H-R-Index，需要基态的*.chk文件，./tdopt/*_freq.chk文件（计算freq）。"
    echo
    echo "Options:"
    echo "  -h    显示帮助信息并退出"
    echo " fcc_HR_index *.chk 文件进行计算"
}

while getopts ":h" opt; do
    case ${opt} in
        h )
            usage
            exit 0
            ;;
        \? )
            echo "Invalid option: -$OPTARG" 1>&2
            usage
            exit 1
            ;;
    esac
done
shift $((OPTIND -1))

if [ $# -eq 0 ]; then
    usage
    exit 1
fi

fcc=$1

mkdir -p "${fcc/.chk/}_HRindex"

cp "$fcc" "./${fcc/.chk/}_HRindex/"
cp "./tdopt/${fcc/.chk/}_tdopt.chk" "./${fcc/.chk/}_HRindex/"

cd "${fcc/.chk/}_HRindex"

# 加载 Intel 环境变量
source /opt/intel/oneapi/setvars.sh

wait
if [ $? -ne 0 ]; then
    echo "加载 Intel 环境变量失败"
    exit 1
fi
# 转换所有 .chk 文件为 .fchk 文件
for i in *.chk; do
    formchk $i
    if [ $? -ne 0 ]; then
        echo "转换 $i 为 .fchk 文件失败"
        exit 1
    fi
done

wait

# 生成 S0,S1 态的 fcc 文件
for m in *.fchk; do
    gen_fcc_state -i $m
    if [ $? -ne 0 ]; then
        echo "生成 $m 的 S0,S1 态的 fcc 文件失败"
        exit 1
    fi
done

# 生成偶极子文件
gen_fcc_dipfile -i ${fcc/.chk/}_tdopt.fchk
if [ $? -ne 0 ]; then
    echo "生成偶极子文件失败"
    exit 1
fi
wait

echo "正在使用 fcclass 计算 HR_index"

# 创建 .inp 文件
cat <<EOL > fcclasses.inp
\$\$\$
PROPERTY     =   EMI  ; OPA/EMI/ECD/CPL/RR/TPA/TPCD/MCD/IC/NRSC
MODEL        =   AH   ; AS/ASF/AH/VG/VGF/VH
DIPOLE       =   FC   ; FC/HTi/HTf
TEMP         =   0.00 ; (temperature in K) 
;DE           = (read) ; (adiabatic/vertical energy in eV. By default, read from state files) 
BROADFUN     =   GAU  ; GAU/LOR/VOI
HWHM         =   0.01 ; (broadening width in eV)
METHOD       =   TD   ; TI/TD
;VIBRATIONAL ANALYSIS 
NORMALMODES  =   COMPUTE   ; COMPUTE/READ/IMPLICIT
COORDS       =   CARTESIAN ; CARTESIAN/INTERNAL
;INPUT DATA FILES 
STATE1_FILE  =   ${fcc/.chk/}_tdopt.fcc    ; 状态 s1 的 fcc 文件
STATE2_FILE  =   ${fcc/.chk/}.fcc          ; 状态 s0 的 fcc 文件
ELDIP_FILE   =   eldip_${fcc/.chk/}_tdopt_fchk ; 状态 s1 的 偶极子 文件
EOL

wait

# 提交计算
fcclasses3 fcclasses.inp

wait

echo "正在调用 deal_HR_index 进行后处理数据"
echo "$(which deal_HR_index)"

deal_HR_index


